/**
 * @file cmd_elogctrl.c
 * @brief 
 * @author  ()
 * @version 1.0
 * @date 2024-09-13
 * 
 * @copyright Copyright (C) 2019 Queclink Wireless Solutions (ShenZhen) Co., Ltd. All Rights Reserved.
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-09-13 <td>1.0     <td>     <td>内容
 * </table>
 */
#include <stdio.h>
#include "lettershell/shell.h"
#include "../shell_log.h"

#include "misc/misc_api.h"

int32_t cmd_elogctrl(int32_t argc, char **argv)
{
    if (argc <= 1) {
        printf("Usage: \r\n"
            "\t elog_ctrl \r\n"
            "\t elog_ctrl {SILENT/ASSERT/ERROR/WARN/INFO/DEBUG/VERBOSE} \r\n"
            "\t elog_ctrl {SILENT/ASSERT/ERROR/WARN/INFO/DEBUG/VERBOSE} {tag)\r\n"
            "Example:\r\n"
            "\t elog_ctrl debug - enable all log\r\n"
            "\t elog_ctrl silent - disable all log\r\n"
            "\t elog_ctrl DEBUG - enable all log below debug level\r\n"
            "\t elog_ctrl DEBUG MODEM - enable all modmem log below debug level\r\n");
    } else if (argc == 2) { /* elog_ctrl level */
        if (0 == strcmp(argv[1], "SILENT") || 0 == strcmp(argv[1], "silent") ||
            0 == strcmp(argv[1], "DISABLE") || 0 == strcmp(argv[1], "disable")) {
            tbox_set_logctrl(ELOG_DISABLE, ELOG_LVL_ASSERT, NULL);
        } else if (0 == strcmp(argv[1], "ASSERT") || 0 == strcmp(argv[1], "assert")) {
            tbox_set_logctrl(ELOG_ENABLE, ELOG_LVL_ASSERT, NULL);
        } else if (0 == strcmp(argv[1], "ERROR") || 0 == strcmp(argv[1], "error")) {
            tbox_set_logctrl(ELOG_ENABLE, ELOG_LVL_ERROR, NULL);
        } else if (0 == strcmp(argv[1], "WARN") || 0 == strcmp(argv[1], "warn")) {
            tbox_set_logctrl(ELOG_ENABLE, ELOG_LVL_WARN, NULL);
        } else if (0 == strcmp(argv[1], "INFO") || 0 == strcmp(argv[1], "info")) {
            tbox_set_logctrl(ELOG_ENABLE, ELOG_LVL_INFO, NULL);
        } else if (0 == strcmp(argv[1], "DEBUG") || 0 == strcmp(argv[1], "debug") ||
            0 == strcmp(argv[1], "ENABLE") || 0 == strcmp(argv[1], "enable")) {
            tbox_set_logctrl(ELOG_ENABLE, ELOG_LVL_DEBUG, NULL);
        } else if (0== strcmp(argv[1], "VERBOSE") || 0 == strcmp(argv[1], "verbose")) {
            tbox_set_logctrl(ELOG_ENABLE, ELOG_LVL_VERBOSE, NULL);
        }
    } else if (argc == 3) { /* elog_ctrl level tag */
        if (0 == strcmp(argv[1], "SILENT") || 0 == strcmp(argv[1], "silent")) {
            tbox_set_logctrl(ELOG_DISABLE, ELOG_LVL_ASSERT, argv[2]);
        } else if (0 == strcmp(argv[1], "ASSERT") || 0 == strcmp(argv[1], "assert")) {
            tbox_set_logctrl(ELOG_ENABLE, ELOG_LVL_ASSERT, argv[2]);
        } else if (0 == strcmp(argv[1], "ERROR") || 0 == strcmp(argv[1], "error")) {
            tbox_set_logctrl(ELOG_ENABLE, ELOG_LVL_ERROR, argv[2]);
        } else if (0 == strcmp(argv[1], "WARN") || 0 == strcmp(argv[1], "warn")) {
            tbox_set_logctrl(ELOG_ENABLE, ELOG_LVL_WARN, argv[2]);
        } else if (0 == strcmp(argv[1], "INFO") || 0 == strcmp(argv[1], "info")) {
            tbox_set_logctrl(ELOG_ENABLE, ELOG_LVL_INFO, argv[2]);
        } else if (0 == strcmp(argv[1], "DEBUG") || 0 == strcmp(argv[1], "debug")) {
            tbox_set_logctrl(ELOG_ENABLE, ELOG_LVL_DEBUG, argv[2]);
        } else if (0== strcmp(argv[1], "VERBOSE") || 0 == strcmp(argv[1], "verbose")) {
            tbox_set_logctrl(ELOG_ENABLE, ELOG_LVL_VERBOSE, argv[2]);
        }
    } else {
        printf("wrong usage of elog_ctrl!\r\n");
    }

    return 0;
}

SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_DISABLE_RETURN,
                 elog_ctrl, 
                 cmd_elogctrl, 
                 elog_ctrl {enable:0/1} {lvl:0~5} config elog level);



/**
 * @brief 
 * @param  on_off           My Param doc
 */
void elogctrl_set_fliter(int32_t argc, char **argv)
{
    if (argc <= 1) {
        printf("Usage: \r\n"
               "\t elog_fliter {TAG} {SILENT/ASSERT/ERROR/WARN/INFO/DEBUG/VERBOSE} \r\n"
               "Example:\r\n"
               "\t elog_fliter modem debug  - enable all log\r\n"
               "\t elog_fliter modem SILENT - disable modem log\r\n"
               "\t elog_fliter modem ERROR  enable all modem log which level equal or below to the error level\r\n");
    } else if (argc == 2) {
        elog_set_filter_tag(argv[1]);
    } else if (argc == 3) {
        if (0 == strcmp(argv[2], "SILENT") || 0 == strcmp(argv[2], "silent")) {
            elog_set_filter_tag_lvl(argv[1], ELOG_FILTER_LVL_SILENT);
        } else if (0 == strcmp(argv[2], "ASSERT") || 0 == strcmp(argv[2], "assert")) {
            elog_set_filter_tag_lvl(argv[1], ELOG_LVL_ASSERT);
        } else if (0 == strcmp(argv[2], "ERROR") || 0 == strcmp(argv[2], "error")) {
            elog_set_filter_tag_lvl(argv[1], ELOG_LVL_ERROR);
        } else if (0 == strcmp(argv[2], "WARN") || 0 == strcmp(argv[2], "warn")) {
            elog_set_filter_tag_lvl(argv[1], ELOG_LVL_WARN);
        } else if (0 == strcmp(argv[2], "INFO") || 0 == strcmp(argv[2], "info")) {
            elog_set_filter_tag_lvl(argv[1], ELOG_LVL_INFO);
        } else if (0 == strcmp(argv[2], "DEBUG") || 0 == strcmp(argv[2], "debug")) {
            elog_set_filter_tag_lvl(argv[1], ELOG_LVL_DEBUG);
        } else if (0 == strcmp(argv[2], "VERBOSE") || 0 == strcmp(argv[2], "verbose")) {
            elog_set_filter_tag_lvl(argv[1], ELOG_LVL_VERBOSE);
        }
    } else {
        printf("Invalid parameter\r\n");
    }
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_DISABLE_RETURN,
                 elog_fliter, 
                 elogctrl_set_fliter, 
                 config elog of TAG on off and level{!config will no save to flash});


#include "global.h"
/**
 * @brief 列举所有可以操作的TAG
 */
void elog_list_tag(void)
{
    printf("TAG list:\r\n");
    printf( TAG_BSP "|"
            TAG_MAIN "|"
            TAG_MODEM "|"
            TAG_AADL "|"
            TAG_HDL "|"
            TAG_GNSS "|"
            TAG_SNSR "|"
            TAG_TBOX "|"
            TAG_SHELL "|"
            TAG_MISC "|"
            TAG_QLIBC "|"
            TAG_COMCU "|"
            TAG_LP);
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_DISABLE_RETURN,
                 elog_list_tag, 
                 elog_list_tag, 
                 list all vaild TAG);